import { ref } from 'vue'
import {
  getWarrantyPolicys as getWarrantyPolicysAPI,
  goodWarrantyPolicyDelete,
  goodWarrantyPolicyMobileShow,
  goodWarrantyPolicysDiable,
} from '@/api/goodsSet'

export const useGetWarrantyPolicys = () => {
  // 获取保修政策列表加载
  const getWarrantyPolicysLoading = ref(false)

  // 保修政策列表
  const warrantyPolicyList = ref<Array<GoodWarrantyPolicy>>([])

  // 获取保修政策列表请求参数
  const getWarrantyPolicysParam = ref<GoodAPIRequest.GetWarrantyPolicys>({
    limit: 50,
    name: '',
    page: 0,
  })

  // 保修政策列表总条数
  const total = ref(0)

  /**
   * 获取保修政策列表
   */
  const getWarrantyPolicys = async () => {
    getWarrantyPolicysLoading.value = true
    const res = await getWarrantyPolicysAPI(getWarrantyPolicysParam.value)
    getWarrantyPolicysLoading.value = false
    warrantyPolicyList.value = res.data.data.rows
    total.value = Number(res.data.data.total)
  }

  return {
    total,
    warrantyPolicyList,
    getWarrantyPolicysParam,
    getWarrantyPolicysLoading,
    getWarrantyPolicys,
  }
}

export const useGoodWarrantyPolicyListDelete = () => {
  // 删除加载
  const deleteLoading = ref(false)

  /**
   * 批量删除保修政策
   * @param {Array<string>} ids 保修政策id集合
   */
  const warrantyPolicyListDelete = async (ids: Array<string>) => {
    deleteLoading.value = true
    const res = await goodWarrantyPolicyDelete(ids)
    deleteLoading.value = false
    return res.data
  }

  return {
    deleteLoading,
    warrantyPolicyListDelete,
  }
}

export const useGoodWarrantyPolicyListEnable = () => {
  // 启用加载
  const enableLoading = ref(false)

  /**
   * 批量启用保修政策
   * @param {Array<string>} ids - 保修政策id集合
   */
  const warrantyPolicyListEnable = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await goodWarrantyPolicysDiable(ids, false)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    warrantyPolicyListEnable,
  }
}

export const useGoodWarrantyPolicyListDisable = () => {
  // 禁用加载
  const disableLoading = ref(false)

  /**
   * 批量禁用保修政策
   * @param {Array<string>} ids - 保修政策id集合
   */
  const warrantyPolicyListDisable = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await goodWarrantyPolicysDiable(ids, true)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    warrantyPolicyListDisable,
  }
}

export const useGoodWarrantyPolicyListMobileShow = () => {
  // 显示加载
  const mobileShowLoading = ref(false)

  /**
   * 批量移动端显示/隐藏保修政策
   * @param {Array<string>} ids - 保修政策id集合
   */
  const warrantyPolicyListMobileShow = async (ids: Array<string>) => {
    mobileShowLoading.value = true
    const res = await goodWarrantyPolicyMobileShow(ids, true)
    mobileShowLoading.value = false
    return res.data
  }

  return {
    mobileShowLoading,
    warrantyPolicyListMobileShow,
  }
}

export const useGoodWarrantyPolicyListMobileHide = () => {
  // 隐藏加载
  const mobileHideLoading = ref(false)

  /**
   * 批量移动端显示/隐藏保修政策
   * @param {Array<string>} ids - 保修政策id集合
   */
  const warrantyPolicyListMobileHide = async (ids: Array<string>) => {
    mobileHideLoading.value = true
    const res = await goodWarrantyPolicyMobileShow(ids, false)
    mobileHideLoading.value = false
    return res.data
  }

  return {
    mobileHideLoading,
    warrantyPolicyListMobileHide,
  }
}
